package com.store.practice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.store.practice.mapper.*;
import com.store.practice.model.*;
import com.store.practice.service.MemberInfoManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MemberInfoManageServiceImpl implements MemberInfoManageService {
    private static final Logger logger = LoggerFactory.getLogger(MemberInfoManageServiceImpl.class);

    @Autowired
    private BgStoreMapper storeMapper;
    @Autowired
    private StoreAdditionMapper storeAdditionMapper;
    @Autowired
    private BgStoreStaffMapper staffMapper;
    @Autowired
    private BgAppointmentOrderMapper orderMapper;
    @Autowired
    private BgStorestaffSkilltagRelationMapper relationMapper;
    @Autowired
    private BgSkillTagMapper tagMapper;

    @Override
    public Map getHome(Integer storeId) {
        Map<String, Object> result = new HashMap<>();
        StoreAdditionExample storeAdditionExample = new StoreAdditionExample();
        storeAdditionExample.createCriteria().andStoreIdEqualTo(storeId);
        List<StoreAddition> storeAdditions = storeAdditionMapper.selectByExample(storeAdditionExample);
        if (storeAdditions==null||storeAdditions.size()==0){
            return null;
        }
        StoreAddition storeAddition = storeAdditions.get(0);
        BgStore bgStore = storeMapper.selectByPrimaryKey(storeId);
        BgStoreStaffExample storeStaffExample = new BgStoreStaffExample();
        storeStaffExample.createCriteria().andStoreIdEqualTo(storeId);
        List<BgStoreStaff> staffList = staffMapper.selectByExample(storeStaffExample);
        ArrayList<Map> bList = new ArrayList<>();
        if (staffList!=null){
            for (BgStoreStaff staff:staffList){
                HashMap<String, Object> map = new HashMap<>();
                BgAppointmentOrderExample orderExample = new BgAppointmentOrderExample();
                orderExample.createCriteria().andBeauticianEqualTo(staff.getName());
                long orderNum = orderMapper.countByExample(orderExample);
                map.put("header",staff.getPicture());
                map.put("name",staff.getName());
                map.put("position",staff.getPosition());
                map.put("orderNum",orderNum);
                bList.add(map);
                CollectionUtil.sort(bList, (o1, o2) -> {
                    long diff = (long)o1.get("orderNum")-(long)o2.get("orderNum");
                    if (diff>0)
                        return -1;
                    else if (diff<0)
                        return 1;
                    return 0;
                });
                logger.info("beauticianList list by orderNum :{}",bList);
            }
        }
        result.put("BgPicture",storeAddition.getPicture());
        result.put("logo",storeAddition.getLogo());
        result.put("storeIntroduction",bgStore.getIntroduction());
        result.put("video",storeAddition.getVideo());
        result.put("beauticianList",bList);
        return result;
    }

    @Override
    public Map getStore(Integer storeId) {
        Map<String, Object> result = new HashMap<>();
        StoreAdditionExample storeAdditionExample = new StoreAdditionExample();
        storeAdditionExample.createCriteria().andStoreIdEqualTo(storeId);
        StoreAddition storeAddition = storeAdditionMapper.selectByExample(storeAdditionExample).get(0);
        if (storeAddition==null){
            return null;
        }
        BgStore bgStore = storeMapper.selectByPrimaryKey(storeId);
        result.put("video",storeAddition.getVideo());
        result.put("storeName",bgStore.getName());
        result.put("businessTime", DateUtil.format(bgStore.getBusinessStartTime(),"HH:mm:ss")
                +"-"+DateUtil.format(bgStore.getBusinessEndTime(),"HH:mm:ss"));
        result.put("address",bgStore.getAddress());
        result.put("contactNum",bgStore.getContactNum());
        result.put("brandIntroduction",storeAddition.getBrandIntd());
        result.put("serviceIntroduction",storeAddition.getServiceIntd());
        return result;
    }

    @Override
    public List<Map> getStaff(Integer storeId) {
        Map<String, Object> map;
        List<Map> result= new ArrayList<>();
        BgStoreStaffExample example = new BgStoreStaffExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        List<BgStoreStaff> staffs = staffMapper.selectByExample(example);
        if (staffs!=null){
            for (BgStoreStaff staff:staffs){
                List<Integer> ids = relationMapper.getTagIdsByStaffId(staff.getId());
                ArrayList<String> skillNames = new ArrayList<>();
                if (ids!=null){
                    for (Integer skillId:ids){
                        String tag = tagMapper.selectByPrimaryKey(skillId).getTag();
                        skillNames.add(tag);
                    }
                }
                map = BeanUtil.beanToMap(staff);
                map.put("header",staff.getPicture());
                map.put("skillNames",skillNames);
                map.put("name",staff.getName());
                map.put("position",staff.getPosition());
                map.put("introduction",staff.getIntroduction());
                result.add(map);
            }
            return result;
        }
        return null;
    }

    @Override
    public int insertOrder(BgAppointmentOrder order) {
        order.setStatus(1);
        return orderMapper.insert(order);
    }

    @Override
    public Map getTime(Date serviceDate,Integer storeId) {
        HashMap<String, Object> result = new HashMap<>();
        BgAppointmentOrderExample example = new BgAppointmentOrderExample();
        BgAppointmentOrderExample.Criteria criteria = example.createCriteria();
        criteria.andServiceDateBetween(DateUtil.beginOfDay(serviceDate),DateUtil.endOfDay(serviceDate));
        criteria.andStoreIdEqualTo(storeId);
        List<BgAppointmentOrder> orders = orderMapper.selectByExample(example);
        result.put("presentTime",DateUtil.format(new Date(),"HH:mm"));
        if (orders!=null){
            ArrayList<Map> timeMaps = new ArrayList<>();
            HashMap<String, String> map = new HashMap<>();
            for (BgAppointmentOrder order:orders){
                map.put("orderedTime",DateUtil.format(order.getStartTime(),"HH:mm")+"-"+DateUtil.format(order.getEndTime(),"HH:mm"));
                timeMaps.add(map);
            }
            result.put("orderedTimeList",timeMaps);
            return result;
        }
        result.put("orderedTimeList",null);
        return result;
    }
}
